using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace OpenComposite.EII.XSDFormGenerator
{
    public partial class FormGeneration : Form
    {
        //private string _XMLSchema = null;
        //private string _XMLData = null;
        //private bool _isOutputSchema;
        //private string _XMLFormStyleData = null;
        private string _displayedForm = "";
        private bool _xmlLoaded = false;
        public FormGeneration()
        {
            InitializeComponent();

        }
        private void SelectTabPage()
        {
            if (DisplayedForm == "Input")
            {
                tabControlSchemas.SelectedTab = tabInput;
                tabControlSchemas_SelectedIndexChanged(tabInput, new EventArgs());
            }
            else
            {
                tabControlSchemas.SelectedTab = tabOutput;
                tabControlSchemas_SelectedIndexChanged(tabOutput, new EventArgs());
            }
        }
        public string DisplayedForm
        {
            set
            {
                _displayedForm = value;
                SelectTabPage();
            }
            get { return _displayedForm; }
        }

        private void tabControlSchemas_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlSchemas.SelectedTab == tabInput)
            {
                btnLoadXML.Visible = true;

                if (_xmlLoaded == false)
                    btnLoadXML.Enabled = true;
                else
                    btnLoadXML.Enabled = false;

                btnSaveXML.Visible = false;
            }
            if (tabControlSchemas.SelectedTab == tabOutput)
            {
                btnSaveXML.Visible = true;
                btnLoadXML.Visible = false;
            }
        }

        private void btnSaveXML_Click(object sender, EventArgs e)
        {
            if (formGeneratorOutput.XML == null || formGeneratorOutput.XML == "")
            {
                MessageBox.Show("No XML data to save.");
                return;
            }

            string xmlFileName = "";

            DialogResult res = saveXMLDialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                xmlFileName = saveXMLDialog.FileName;
                //StreamWriter sw=new StreamWriter(xmlFileName, false, System.Text.Encoding.UTF8);
                StreamWriter sw = new StreamWriter(xmlFileName, false);
                sw.Write(formGeneratorOutput.XML);
                sw.Flush();
                sw.Close();
            }
        }

        private void btnLoadXML_Click(object sender, EventArgs e)
        {
            string xmlFileName = "";

            loadXMLDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
            DialogResult res = loadXMLDialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                xmlFileName = loadXMLDialog.FileName;
                StreamReader sr = new StreamReader(loadXMLDialog.FileName);
                formGeneratorInput.XML = sr.ReadToEnd();
                sr.Close();

                _xmlLoaded = true;
                tabControlSchemas_SelectedIndexChanged(tabInput, new EventArgs());
            }
        }



        //public TestForm1(string XMLSchema, string XMLData, bool isOutputSchema, string XMLFormStyleData)
        //{
        //    Init(XMLSchema, XMLData, isOutputSchema, XMLFormStyleData);
        //    InitializeComponent();
        //}
        //private void Init(string XMLSchema, string XMLData, bool isOutputSchema, string XMLFormStyleData)
        //{
        //    _XMLSchema        = XMLSchema;
        //    _XMLData          = XMLData;
        //    _isOutputSchema   = isOutputSchema;
        //    _XMLFormStyleData = XMLFormStyleData;


        //}
    }
}
    